<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\CateService;
use Illuminate\Http\Request;

class CateController extends BaseController
{
    //业务

    public function __construct(CateService $caseService)
    {
        $this->db = $caseService;
    }

    //选项
    public function caseOption(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $res = $this->db->caseOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function caseIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $pid = $request->get('pid', 0);
        $res = $this->db->caseIndex($pid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //更新
    public function modify(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->get('id', 0);
        $pas['title'] = $request->get('title', '');
        $pas['pid'] = $request->get('pid', 0);
        if ($id) {
            $res = $this->db->caseModify($id, $pas);
        } else {
            $res = $this->db->caseAdd($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->get('id', 0);
        $res = $this->db->caseDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}